<?php
/**
 * Description: 门店主页service
 * Created by IntelliJ IDEA.
 * User: 玉坤
 * Date: 2021/7/20
 * Time: 14:32
 */

namespace Biz\service\Store;


use Biz\model\store\StoreBaseDecorationModel;
use Biz\model\store\StoreCaseModel;
use Biz\model\store\StoreImagesModel;
use Biz\model\store\StoreSaleModel;
use Biz\model\store\StoreVideoModel;
use Doraemon\pockets\constInterface\FormatScene;
use Doraemon\service\BlankStoreCaseRelation;
use Doraemon\service\Format\Blank\HandleBlank;
use \Biz\model\store\StoreBasic as mStore;

class StoreHomeService
{
    /**
     * 获取图片列表（可扩展条件）
     * @param $store_id
     * @param string $field
     * @param array $where_ext
     * @return array
     */
    public static function getImageList($store_id, $field = '*', array $where_ext = []): array
    {
        $where['store_id'] = $store_id;
        $where['ORDER'] = ['top_time' => "DESC", 'id' => 'DESC'];
        $where = array_merge($where, $where_ext);
        return StoreImagesModel::all($where, $field);
    }

    /**
     * 获取视频列表（可扩展条件）
     * @param $store_id
     * @param string $field
     * @param array $where_ext
     * @return array
     */
    public static function getVideoList($store_id, $field = '*', array $where_ext = []): array
    {
        $where['store_id'] = $store_id;
//        $where['check_status'] = StoreImagesModel::PASS;
        $where['ORDER'] = ['top_time' => "DESC", 'id' => 'DESC'];
        $where = array_merge($where, $where_ext);
        return StoreVideoModel::all($where, $field);
    }


    /**
     * 获取真是案例列表（可扩展条件）
     * @param $store_id
     * @param string $field
     * @param array $where_ext
     * @return array
     */
    public static function getCaseList($store_id, $field = '*', array $where_ext = []): array
    {
        $where['store_id'] = $store_id;
        $where['ORDER'] = ['top_time' => "DESC", 'id' => 'DESC'];
        $where = array_merge($where, $where_ext);
        $list = StoreCaseModel::all($where, $field);
        if (!$list) {
            return [];
        }
        $store_category = mStore::getField($store_id, 'store_category') ?: '';
        $case_id = array_column($list, 'case_id');
        $blank_lists = HandleBlank::handleBlank($case_id, FormatScene::BASE_INFO);
        if (in_array($store_category, ["设备机电", '其他品类'])) {
            $store_category = '';
        }
        $new_list = [];
        foreach ($list as $k => $item) {
            $new_list[$k]['id'] = $item['id'];
            $new_list[$k]['store_id'] = $item['store_id'];
            $new_list[$k]['case_id'] = $item['case_id'];
            $new_list[$k]['house_area'] = $item['house_area'];
            $new_list[$k]['house_price'] = $item['house_price'];
            $price = $item['house_price'] / 10000;
            $sec_title = floor($item['house_area']) . '平米 . ' . $store_category;

            if ($price < 1) {
                $sec_title .= "花费" . floor($item['house_price']) . '元';
            } else {
                $sec_title .= "花费" . bcdiv($price, 1, 1) . '万';
            }

            $new_list[$k]['sec_title'] = $sec_title;
            $new_list[$k]['store_category'] = $store_category ?? '';
            $new_list[$k]['title'] = $blank_lists[$item['case_id']]['blank']['blank_info']['title'] ?? '';
            $new_list[$k]['cover_pic_url'] = $blank_lists[$item['case_id']]['blank']['blank_info']['cover_pic_url'] ?? '';
//            $list[$k]['case_info'] = $blank_lists[$item['case_id']] ?? [];
        }
        return array_values($new_list) ?: [];
    }

    /**
     * 获取优惠信息（可扩展条件）
     * @param $store_id
     * @param array $where_ext
     * @param string $field
     * @return array
     */
    public static function getSaleList($store_id, array $where_ext = [], $field = "*"): array
    {
        $where['store_id'] = $store_id;
        $where = array_merge($where, $where_ext);
        return StoreSaleModel::all($where, $field);
    }

    /**
     * 获取门店优惠信息
     * @param $store_id
     * @return array
     */
    public static function getStoreSaleInfo($store_id): array
    {
        $where['content[!]'] = '';
        $field = ['content', 'sale_type'];
        return self::getSaleList($store_id, $where, $field) ?: [];
    }


    /**
     * 获取门店问答开启状态
     * @param $store_id
     * @param array $where_ext
     * @return array|int|mixed
     */
    public static function getIssueStatus($store_id, array $where_ext = [])
    {
        $where['store_id'] = $store_id;
        $where['des_type'] = StoreBaseDecorationModel::ISSUE;
        $where = array_merge($where, $where_ext);
        $issue_status = StoreBaseDecorationModel::first($where, 'status');
        if (is_null($issue_status)) {
            return StoreBaseDecorationModel::OFF;
        }
        return $issue_status;
    }

    /**
     * 保存主页装修模块
     * @param $store_id
     * @param $des_type
     * @return false|int|mixed|string
     */
    public static function saveHomeModel($store_id, $des_type)
    {
        $where['store_id'] = $store_id;
        $where['des_type'] = $des_type;
        $isset = StoreBaseDecorationModel::value($where);
        if ($isset) {
            return true;
        }
        $data['store_id'] = $store_id;
        $data['des_type'] = $des_type;
        $data['status'] = StoreBaseDecorationModel::ON;
        return StoreBaseDecorationModel::add($data);
    }

    /**
     * 获取obj_ids
     * @param $store_id
     * @return array|bool
     */
    public static function getObjIds($store_id)
    {
        $where['store_id'] = $store_id;
        $where['check_status'] = StoreCaseModel::PASS;
        return StoreCaseModel::pluck($where, 'case_id');
    }

    /**
     * 更新门店品类（社区）
     * @param $store_id
     * @param $store_category
     * @return bool
     */
    public static function updateStoreCategoryForCase($store_id, $store_category): bool
    {
        $case_ids = StoreCaseModel::pluck(['store_id' => $store_id], 'case_id');
        $save_blank['store_category'] = $store_category;
        BlankStoreCaseRelation::saveInfo($save_blank, $case_ids);
        return true;
    }


    /*删除 案例id
     * @param $obj_id
     * @return bool
     * */
    public static function delStoreCase($case_id)
    {

        return StoreCaseModel::delByCaseId($case_id);
    }


}